<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>serializer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_proto2_serializer.js.html">serializer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class for all Protocol Buffer 2 serializers.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.proto2.Serializer&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.proto2.Descriptor&#39;);
<a name="line22"></a>goog.require(&#39;goog.proto2.FieldDescriptor&#39;);
<a name="line23"></a>goog.require(&#39;goog.proto2.Message&#39;);
<a name="line24"></a>goog.require(&#39;goog.proto2.Util&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Abstract base class for PB2 serializers. A serializer is a class which
<a name="line30"></a> * implements the serialization and deserialization of a Protocol Buffer Message
<a name="line31"></a> * to/from a specific format.
<a name="line32"></a> *
<a name="line33"></a> * @constructor
<a name="line34"></a> */
<a name="line35"></a>goog.proto2.Serializer = function() {};
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>/**
<a name="line39"></a> * Serializes a message to the expected format.
<a name="line40"></a> *
<a name="line41"></a> * @param {goog.proto2.Message} message The message to be serialized.
<a name="line42"></a> *
<a name="line43"></a> * @return {*} The serialized form of the message.
<a name="line44"></a> */
<a name="line45"></a>goog.proto2.Serializer.prototype.serialize = goog.abstractMethod;
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>/**
<a name="line49"></a> * Returns the serialized form of the given value for the given field
<a name="line50"></a> * if the field is a Message or Group and returns the value unchanged
<a name="line51"></a> * otherwise.
<a name="line52"></a> *
<a name="line53"></a> * @param {goog.proto2.FieldDescriptor} field The field from which this
<a name="line54"></a> *     value came.
<a name="line55"></a> *
<a name="line56"></a> * @param {*} value The value of the field.
<a name="line57"></a> *
<a name="line58"></a> * @return {*} The value.
<a name="line59"></a> * @protected
<a name="line60"></a> */
<a name="line61"></a>goog.proto2.Serializer.prototype.getSerializedValue = function(field, value) {
<a name="line62"></a>  if (field.isCompositeType()) {
<a name="line63"></a>    return this.serialize(/** @type {goog.proto2.Message} */ (value));
<a name="line64"></a>  } else {
<a name="line65"></a>    return value;
<a name="line66"></a>  }
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Deserializes a message from the expected format.
<a name="line72"></a> *
<a name="line73"></a> * @param {goog.proto2.Descriptor} descriptor The descriptor of the message
<a name="line74"></a> *     to be created.
<a name="line75"></a> * @param {*} data The data of the message.
<a name="line76"></a> *
<a name="line77"></a> * @return {goog.proto2.Message} The message created.
<a name="line78"></a> */
<a name="line79"></a>goog.proto2.Serializer.prototype.deserialize = function(descriptor, data) {
<a name="line80"></a>  var message = descriptor.createMessageInstance();
<a name="line81"></a>  this.deserializeTo(message, data);
<a name="line82"></a>  goog.proto2.Util.assert(message instanceof goog.proto2.Message);
<a name="line83"></a>  return message;
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Deserializes a message from the expected format and places the
<a name="line89"></a> * data in the message.
<a name="line90"></a> *
<a name="line91"></a> * @param {goog.proto2.Message} message The message in which to
<a name="line92"></a> *     place the information.
<a name="line93"></a> * @param {*} data The data of the message.
<a name="line94"></a> */
<a name="line95"></a>goog.proto2.Serializer.prototype.deserializeTo = goog.abstractMethod;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Returns the deserialized form of the given value for the given field if the
<a name="line100"></a> * field is a Message or Group and returns the value, converted or unchanged,
<a name="line101"></a> * for primitive field types otherwise.
<a name="line102"></a> *
<a name="line103"></a> * @param {goog.proto2.FieldDescriptor} field The field from which this
<a name="line104"></a> *     value came.
<a name="line105"></a> *
<a name="line106"></a> * @param {*} value The value of the field.
<a name="line107"></a> *
<a name="line108"></a> * @return {*} The value.
<a name="line109"></a> * @protected
<a name="line110"></a> */
<a name="line111"></a>goog.proto2.Serializer.prototype.getDeserializedValue = function(field, value) {
<a name="line112"></a>  // Composite types are deserialized recursively.
<a name="line113"></a>  if (field.isCompositeType()) {
<a name="line114"></a>    if (value instanceof goog.proto2.Message) {
<a name="line115"></a>      return value;
<a name="line116"></a>    }
<a name="line117"></a>
<a name="line118"></a>    return this.deserialize(field.getFieldMessageType(), value);
<a name="line119"></a>  }
<a name="line120"></a>
<a name="line121"></a>  // Return the raw value if the field does not allow the JSON input to be
<a name="line122"></a>  // converted.
<a name="line123"></a>  if (!field.deserializationConversionPermitted()) {
<a name="line124"></a>    return value;
<a name="line125"></a>  }
<a name="line126"></a>
<a name="line127"></a>  // Convert to native type of field.  Return the converted value or fall
<a name="line128"></a>  // through to return the raw value.  The JSON encoding of int64 value 123
<a name="line129"></a>  // might be either the number 123 or the string &quot;123&quot;.  The field native type
<a name="line130"></a>  // could be either Number or String (depending on field options in the .proto
<a name="line131"></a>  // file).  All four combinations should work correctly.
<a name="line132"></a>  var nativeType = field.getNativeType();
<a name="line133"></a>  if (nativeType === String) {
<a name="line134"></a>    // JSON numbers can be converted to strings.
<a name="line135"></a>    if (typeof value === &#39;number&#39;) {
<a name="line136"></a>      return String(value);
<a name="line137"></a>    }
<a name="line138"></a>  } else if (nativeType === Number) {
<a name="line139"></a>    // JSON strings are sometimes used for large integer numeric values.
<a name="line140"></a>    if (typeof value === &#39;string&#39;) {
<a name="line141"></a>      // Validate the string.  If the string is not an integral number, we would
<a name="line142"></a>      // rather have an assertion or error in the caller than a mysterious NaN
<a name="line143"></a>      // value.
<a name="line144"></a>      if (/^-?[0-9]+$/.test(value)) {
<a name="line145"></a>        return Number(value);
<a name="line146"></a>      }
<a name="line147"></a>    }
<a name="line148"></a>  }
<a name="line149"></a>
<a name="line150"></a>  return value;
<a name="line151"></a>};
</pre>


</body>
</html>
